package com.xd.cloud.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.entity.core.Team;
import com.xd.cloud.entity.vehicle.VehicleInfo;
import com.xd.cloud.entity.vehicle.VehiclePo;
import com.xd.cloud.service.device.TeamService;
import com.xd.cloud.service.vehicle.VehicleInfoService;

/**   
 * 功能描述：车队管理controller
 * @author: wangshanshan   
 * @date: 2017年12月14日 上午16:30:28 
 */
@Controller
@RequestMapping("/team")
public class TeamController extends BaseController{

	@Autowired
	private TeamService teamService;
	@Autowired
	private VehicleInfoService vehicleInfoService;
	
	/**
	 * 添加or修改数据
	 * @return Object
	 */
	@RequestMapping(value = "/saveOrUpdateInfo")
	@ResponseBody
	public Object saveOrUpdateInfo(HttpServletRequest request, HttpServletResponse response,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			Team item = new Team();
			if(StringUtils.isNotEmpty(id)) {
			    String company_id = request.getParameter("company_id");
			    String name = request.getParameter("name");
			    item.setName(name);
			    item.setCompany_id(Integer.valueOf(company_id));
				item.setUpdate_time(new Date());
				if(id.equals("-1")) {//添加操作
					item.setCreate_time(new Date());
					teamService.save(item);
					msgBean.setSuccess(true);
				}else {//修改操作
					Team temp = teamService.getTeamById(Integer.valueOf(id));
					item.setId(Integer.valueOf(id));
					item.setCreate_time(temp.getCreate_time());
					teamService.update(item);
					msgBean.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 删除操作
	 * @return Object
	 */
	@RequestMapping(value = "/deleteInfo")
	@ResponseBody
	public Object deleteInfo(HttpServletRequest request,String id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(id)) {
				boolean flag = teamService.deleteInfo(Integer.valueOf(id));
				msgBean.setSuccess(flag);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return msgBean;
	}

	/**
	 * 所属公司但未分配车队的列表
	 * @return Object
	 */
	@RequestMapping(value = "/getVehicleByCompanyIdNoTeam")
	@ResponseBody
	public Object getVehicleByCompanyIdNoTeam(HttpServletRequest request, HttpServletResponse response,String company_id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(company_id)) {
				List<VehiclePo> list = vehicleInfoService.getVehicleByCompanyIdNoTeam(Integer.valueOf(company_id));
				if(list!=null && list.size()>0) {
					msgBean.setSuccess(true);
					msgBean.setMsg(list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	
	/**
	 * 已分配车队的车辆列表
	 * @return Object
	 */
	@RequestMapping(value = "/getVehicleByTeamId")
	@ResponseBody
	public Object getVehicleByTeamId(HttpServletRequest request, HttpServletResponse response,String team_id) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(team_id)) {
				List<VehiclePo> list = vehicleInfoService.getVehicleByTeamId(Integer.valueOf(team_id));
				if(list!=null && list.size()>0) {
					msgBean.setSuccess(true);
					msgBean.setMsg(list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	/**
	 * 移至车队
	 * @return Object
	 */
	@RequestMapping(value = "/removeInTeam")
	@ResponseBody
	public Object removeInTeam(HttpServletRequest request, HttpServletResponse response,String team_id,String chkIds) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(team_id) && StringUtils.isNotEmpty(chkIds)) {
				int teamId = Integer.valueOf(team_id);
				String[] chkAry = chkIds.split(",");
				if(chkAry!=null && chkAry.length>0) {
					for(int i=0;i<chkAry.length;i++) {
						vehicleInfoService.removeInTeam(teamId,Integer.valueOf(chkAry[i]));
					}
					msgBean.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}	
	/**
	 * 移出车队
	 * @return Object
	 */
	@RequestMapping(value = "/removeOutTeam")
	@ResponseBody
	public Object removeOutTeam(HttpServletRequest request, HttpServletResponse response,String team_id,String chkIds) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(team_id) && StringUtils.isNotEmpty(chkIds)) {
				int teamId = Integer.valueOf(team_id);
				String[] chkAry = chkIds.split(",");
				if(chkAry!=null && chkAry.length>0) {
					for(int i=0;i<chkAry.length;i++) {
						vehicleInfoService.removeOutTeam(teamId,Integer.valueOf(chkAry[i]));
					}
					msgBean.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
}
